<?php

namespace App\Domain\ApplicationCenter\Controllers;


use App\Domain\ApplicationCenter\Models\ApplicationCenterModel;
use App\Domain\ApplicationCenter\Requests\CreateAppPost;
use App\Domain\ApplicationCenter\Services\ApplicationCenterService;
use App\Http\Controllers\Controller;
use App\Supports\Constant\PaginationConst;
use App\Supports\Formatter\ResponseFormatter;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

class ApplicationCenterController extends Controller
{
    public function createApp(CreateAppPost $request, ApplicationCenterService $applicationCenterService)
    {
        $param = $request->validated();
        $res = $applicationCenterService->create($param);
        return $this->success($res);
    }

    public function list(Request $request, ApplicationCenterService $applicationCenterService)
    {
        $this->validate($request, [
            'name' => 'string|nullable',
            'currentPage' => 'integer|min:1|nullable',
            'pageSize' => 'integer|min:1|nullable',
        ]);
        $param = [
            'name' => $request->input('name'),
            'currentPage' => $request->input('currentPage', 1),
            'pageSize' => $request->input('pageSize', PaginationConst::PAGE_SIZE),
        ];

        $app = $applicationCenterService->list($param);
        $app = new LengthAwarePaginator($app->items(), $app->total(), $app->perPage(), $app->currentPage());
        //格式化
        $keyMap = [
            'id' => 'id',
            'name' => 'name',
            'icon' => 'icon',
            'language_arousal' => 'language_arousal',
            'startup_parameter' => 'startup_parameter',
            'download_url' => 'download_url',
            'description' => 'description',
            'sort' => 'sort',
        ];
        return $this->success(ResponseFormatter::formatterPaginator($app, $keyMap));
    }

    public function del(Request $request, ApplicationCenterService $applicationCenterService)
    {
        $data = $applicationCenterService->del($request['id']);
        return $this->success($data);
    }

    public function updateApp(Request $request, ApplicationCenterService $applicationCenterService)
    {
        $this->validate($request, [
            'id' => 'required',
            'name' => 'required',
            'icon' => 'required',
            'language_arousal' => 'required',
            'startup_parameter' => 'required',
            'download_url' => 'required',
            'description' => '',
            'sort' => '',
        ]);
        $param = [
            'id' => $request['id'],
            'name' => $request['name'],
            'icon' => $request['icon'],
            'language_arousal' => $request['language_arousal'],
            'startup_parameter' => $request['startup_parameter'],
            'download_url' => $request['download_url'],
            'description' => $request['description'],
            'sort' => $request['sort'],
        ];
        $data = $applicationCenterService->update($param);
        return $this->success($data);
    }
}
